package GUI;

import 数据库和网络.myDatebase;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class addSubjectiveWindow extends JFrame {
    JPanel mainPanel;
    JScrollPane qScroller,answerScroller;
    JButton addButton,backButton;
    JLabel questionCont ;
    JLabel answerCont ;
    JTextArea title,difficulty,score,questionContent,answer,answerContent;

    public static void main(String[] args) {
        new addSubjectiveWindow();
    }
    public addSubjectiveWindow(){
        mainPanel= new JPanel();
        mainPanel.setLayout(null);
        questionCont = new JLabel("题干：");
        answerCont = new JLabel("参考答案：");
        addButton = new JButton("添加");
        backButton = new JButton("返回");


        JLabel questionTitle = new JLabel("标题");
        title = new JTextArea(1,5);
        JLabel questionDifficulty = new JLabel("难度：");
        difficulty = new JTextArea(1,5);
        JLabel questionScore = new JLabel("满分：");
        score = new JTextArea(1,5);

        addButton.setBackground(Color.lightGray);
        addButton.addActionListener(new addListener());
        backButton.setBackground(Color.lightGray);
        backButton.addActionListener(new backListener());

        answer = new JTextArea(1,5);

        questionContent = new JTextArea(6,20);
        questionContent.setText("qContent");
        questionContent.setLineWrap(true);
        questionContent.setWrapStyleWord(true);
        //questionContent.setFont(bigFont);

        qScroller = new JScrollPane(questionContent);
        qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        answerContent = new JTextArea(1,20);
        answerContent.setText("answerContent");
        answerContent.setLineWrap(true);
        answerContent.setWrapStyleWord(true);
        //questionContent.setFont(bigFont);

        answerScroller = new JScrollPane(answerContent);
        answerScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        answerScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);


        questionTitle.setBounds(30,30,50,20);
        title.setBounds(120,30,300,30);
        questionCont.setBounds(30,70,60,20);
        qScroller.setBounds(120,70,300,100);
        answerCont.setBounds(30,180,80,20);
        answerScroller.setBounds(120,180,300,100);
        questionDifficulty.setBounds(30,290,50,20);
        difficulty.setBounds(120,290,300,30);
        questionScore.setBounds(30,330,50,20);
        score.setBounds(120,330,300,30);
        addButton.setBounds(120,390,70,30);
        backButton.setBounds(300,390,70,30);

        mainPanel.add(questionTitle);
        mainPanel.add(title);
        mainPanel.add(questionCont);
        mainPanel.add(qScroller);
        mainPanel.add(answerCont);
        mainPanel.add(answerScroller);
        mainPanel.add(addButton);
        mainPanel.add(backButton);
        mainPanel.add(questionDifficulty);
        mainPanel.add(difficulty);
        mainPanel.add(questionScore);
        mainPanel.add(score);

        this.setTitle("添加简答题");
        this.setContentPane(mainPanel);
        this.setSize(490,490);
        mainPanel.setSize(this.getSize());
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        //this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    }
    public class backListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {//关闭注册界面
            addSubjectiveWindow.this.dispose();

        }
    }
    public class addListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {//关闭注册界面

            myDatebase editor=new myDatebase("questioncollection");
            String sql="insert into questioncollection.subjective_question ( questiontitle, questiontext, questiondifficulties, fullscore, answer) " +
                    "values ('"+title.getText()+"','"+questionContent.getText()+"','"+difficulty.getText()+"','"+score.getText()+"','"+answerContent.getText()+"')";
            try {
                editor.statement.execute(sql);
            } catch (SQLException throwables) {
                new warningForSyntexWindow("a");
                throwables.printStackTrace();
            }
            editor.close();

            addSubjectiveWindow.this.dispose();
            questionWindow.mainPanel.removeAll();
            questionWindow.mainPanel.updateUI();
        }
    }
}
